package com.xmb.wechat.view.wechat.chat;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.bean.WechatLastMsgBean_;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.bean.WechatMsgBean_;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.delegate.GroupTalkDelegate;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/14 16:53
 */
public class WechatQunliaoActivity extends BaseActivity {
    private static final int REQUEST_ADD_GROUP  = 101;
    private static final int REQUEST_EDIT_GROUP = 102;

    @BindView(R2.id.ll_hint)
    LinearLayout mLlHint;

    private GroupTalkDelegate mDelegate;

    private Box<GroupTalkBean>     mGroupTalkBox;
    private Box<WechatLastMsgBean> mLastMsgBeanBox;

    public WechatQunliaoActivity() {
        super(R.layout.activity_wechat_qunliao);
    }

    @Override
    protected void initView() {
        mGroupTalkBox = WechatStartApplication.getBoxStore(this).boxFor(GroupTalkBean.class);
        mLastMsgBeanBox = WechatStartApplication.getBoxStore().boxFor(WechatLastMsgBean.class);
        //init delegate
        mDelegate = new GroupTalkDelegate(this, mLastMsgBeanBox);
        mDelegate.getAdapter().setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                GroupTalkBean item = (GroupTalkBean) mDelegate.getData().get(position);
                new AlertDialog.Builder(WechatQunliaoActivity.this).setItems(new String[]{"编辑", "删除"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                WechatQunliaoAddActivity.start4Result(WechatQunliaoActivity.this, item, REQUEST_EDIT_GROUP);
                                break;
                            case 1:
                                //删除群聊数据
                                mGroupTalkBox.remove(item);
                                //删除首页消息数据
                                WechatLastMsgBean latestMsg = mLastMsgBeanBox.query().equal(WechatLastMsgBean_.isRoomChat, true)
                                        .equal(WechatLastMsgBean_.talkerID, item.getId()).build().findUnique();
                                if (latestMsg != null)
                                    mLastMsgBeanBox.remove(latestMsg);
                                //删除群聊消息记录
                                Box<WechatMsgBean> wechatMsgBeanBox = WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class);
                                if (wechatMsgBeanBox != null) {
                                    List<WechatMsgBean> msgs = wechatMsgBeanBox.query().equal(WechatMsgBean_.talkerID, item.getId()).equal(WechatMsgBean_.isRoomChat, true).build().find();
                                    wechatMsgBeanBox.remove(msgs);
                                }
                                refresh();
                                break;
                        }
                    }
                }).setNegativeButton("取消", null).show();
                return true;
            }
        });
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatQunliaoActivity.this.finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        List<GroupTalkBean> datas = mGroupTalkBox.query().build().find();
        if (datas.size() > 0) {
            mDelegate.clearData();
            mDelegate.addData(datas);
            showEmptyView(false);
            mLlHint.setVisibility(View.VISIBLE);
        } else {
            showEmptyView(true);
            mLlHint.setVisibility(View.GONE);
        }
    }

    @OnClick({R2.id.ll_hint, R2.id.btn_add})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_hint) {
            mLlHint.setVisibility(View.GONE);
        } else if (id == R.id.btn_add) {
            WechatQunliaoAddActivity.start4Result(WechatQunliaoActivity.this, REQUEST_ADD_GROUP);
        }
    }
}
